package one.bugu.android.demon.ui.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.View;
import android.widget.*;

import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import one.bugu.android.demon.R;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.ui.fragment.CoinFragment;
import one.bugu.android.demon.ui.fragment.SearchResultFragment;
import one.bugu.android.demon.ui.widget.BaseTopBarView;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;

/**
 * 添加币的acticity
 */
@LKContentView(R.layout.activity_addcoin)
public class AddCoinActivity extends MyBaseActivity {
    /**
     * topBar
     */
    @LKInjectView(R.id.btbv_add_coin)
    private BaseTopBarView btbvAddCoin;

    /**
     * 搜索图片
     */
    @LKInjectView(R.id.serach)
    private ImageView serach;

    /**
     * 替换界面的FrameLayout
     */
    @LKInjectView(R.id.fragment)
    private FrameLayout fragment;

    /**
     * 搜索编辑框
     */
    @LKInjectView(R.id.edt_coin)
    private EditText searchResult;

    /**
     * 新用于下一步按钮
     */
    @LKInjectView(R.id.addCoin_next)
    private Button addCoinNext;
    /**
     * 根据平台添加币种的fragment
     */
    private CoinFragment fg_bizhong;

    /**
     * 搜索结果的fragment
     */
    private SearchResultFragment fg_searchR;
    private FragmentManager fragmentManager;
    private ActionBarDrawerToggle drawerbar;


    @Override
    protected void initView() {
        super.initView();
        //设置状态栏
        StatusBarUtils.getInstance().setStatusBar("#FFFFFF", this, btbvAddCoin, false);

        //默认显示根据平台添加币种的fragment
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        fg_bizhong = new CoinFragment();
        fragmentTransaction.add(R.id.fragment, fg_bizhong);
        fragmentTransaction.commit();
    }

    @Override
    protected void initData() {
        super.initData();
        //判断新老用户
        boolean isNew = getIntent().getBooleanExtra("isNew", false);

        fg_bizhong.setFlag(isNew);

        //新用户
        if (isNew) {
            addCoinNext.setVisibility(View.VISIBLE);
            //点击下一步 跳转预警主界面
            addCoinNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    IntentUtils.startAty(AddCoinActivity.this, WarnMainActivity.class, Constant.ID, 0);
                    finish();
                }
            });
        } else {
            //老用户 隐藏下一步按钮
            addCoinNext.setVisibility(View.GONE);
        }
    }


    @Override
    protected void initEvent() {
        super.initEvent();

        //搜索点击事件
        serach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //显示搜索结果的fragment
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                hideAllFragment(fragmentTransaction);

                fg_searchR = new SearchResultFragment();

                fragmentTransaction.add(R.id.fragment, fg_searchR);

                fragmentTransaction.commit();

                PreferencesUtil.getInstance().putString(AddCoinActivity.this, Constant.KEYWORD, searchResult.getText().toString());
            }
        });

        //返回点击事件
        btbvAddCoin.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                finish();
            }

            @Override
            public void leftCloseClick() {

            }

            @Override
            public void rightOnClick() {

            }
        });

    }

    /**
     * 隐藏fragment界面
     *
     * @param fragmentTransaction
     */
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (fg_bizhong != null) {
            fragmentTransaction.hide(fg_bizhong);
        }

        if (fg_searchR != null) {
            fragmentTransaction.hide(fg_searchR);
        }
    }

    /**
     * 手机返回键刷新页面
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();//注销该方法，相当于重写父类这个方法
        finish();
    }
}
